----------------客户端对象池-------------------

local IsClient = game:GetService("RunService"):IsClient();




local CommonOHOH = _G.CommonOHOH;

local OBJ_POOL={}
local ID_PERFAB={}

local POOL_DATA=require(script:WaitForChild("data"))
local POOL_FOLDER=Instance.new("Folder",workspace)

if  IsClient then
	POOL_FOLDER.Name="OBJPOOL_Client"
else
	POOL_FOLDER.Name="OBJPOOL_Server"
end


--创建对象池
function CreatPool(count,perfab,setPosFuc,folder)
	local pool={}
	local parent=folder or game.Workspace
	
	for i=1,count do
		local obj=perfab:Clone()
		obj.Parent=parent
		if setPosFuc then
			setPosFuc(obj)
		end
		table.insert(pool,obj)
	end
	OBJ_POOL[perfab]=pool
	return pool
end

--根据数据创建对象池
function CreatPoolByData(data)
	
	for k,v in pairs(data) do
		
		local perfab=CommonOHOH:GetPath_Loop(v.path, game.ReplicatedStorage.Resource)
		local f=POOL_FOLDER
		if v.folder then
			f=workspace:FindFirstChild(v.folder)
			if not f then
				f=Instance.new("Folder",workspace)
				f.Name=v.folder
			end
		end
		v.folder=f
		if v.init then
			v.init(perfab)
		end
		CreatPool(v.count,perfab,v.SetPos,v.folder)
		ID_PERFAB[k]=perfab
	end
end

--获取对象
function GetObjById(id)
	
	assert(ID_PERFAB[id],"ID_PERFAB[id]==nil"..id)
	local perfab=ID_PERFAB[id]
	local pool=OBJ_POOL[perfab]
	local lenght=#pool
	
	if lenght>0 then
		local obj=pool[lenght]
		table.remove(pool,lenght)
		return obj
	end
	local obj= perfab:Clone()
	obj.Parent=POOL_DATA[id].folder
	return obj

end

--重置对象坐标
function RestObjPosById(id,obj)
	POOL_DATA[id].SetPos(obj)
end


--回收对象
function PushObjById(id,obj)
	assert(ID_PERFAB[id],"ID_PERFAB[id]==nil"..id)
	local perfab=ID_PERFAB[id]
	local pool=OBJ_POOL[perfab]
	table.insert(pool,obj)
	obj.Parent=POOL_DATA[id].folder
	POOL_DATA[id].SetPos(obj)
end

--创建对象池
CreatPoolByData(POOL_DATA)

local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());

function M.new(self)
	return M(self);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
end;

function M.dtor(self)
	
	for k,v in pairs(POOL_DATA) do
		v.folder:Destroy()
	end
	
	OBJ_POOL={}
	ID_PERFAB={}
	POOL_DATA={}
	
	M.super.dtor(self);
end;

function M.GetObj(self,id)
	return GetObjById(id)
end;

function M.PushObj(self,id,obj)
	return PushObjById(id,obj)
end;

function M.ResetObjPos(self,id,obj)
	RestObjPosById(id,obj)
end;

-- 参数格式：
--{
--	["b1.boom"]={
--		folder="BoomPool_Client",
--		path="Perfabs.Effect.Boom.b1.boom",
--		count=90,
--		SetPos=function(obj)
--			obj:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,-100,0)))
--		end,
--	},
function M.CreatePool(self,data)
	for k,v in pairs(data) do
		if POOL_DATA[k] then
			local perfab=ID_PERFAB[k]
			local pool=OBJ_POOL[perfab]
			assert(perfab and pool,"POOL DATA Error")
			
			for i=1,v.count do
				local obj=perfab:Clone()
				obj.Parent=POOL_DATA[k].folder
				POOL_DATA[k].SetPos(obj)
				table.insert(pool,obj)
			end
			
		else
			local perfab=CommonOHOH:GetPath_Loop(v.path, game.ReplicatedStorage.Resource)
			local f=POOL_FOLDER
			if v.folder then
				f=workspace:FindFirstChild(v.folder)
				if not f then
					f=Instance.new("Folder",workspace)
					f.Name=v.folder
				end
			end
			v.folder=f
			CreatPool(v.count,perfab,v.SetPos,v.folder)
			ID_PERFAB[k]=perfab
			POOL_DATA[k]=v
		end


	end
end

return M;